#!/bin/bash
set -e -o pipefail

git clone --depth 1 https://github.com/keystone-engine/keystone.git

source ctf-tools-venv-activate

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

mkdir -p keystone/build/
pushd keystone/build/
cmake -DCMAKE_INSTALL_PREFIX="$DIR" \
	-DCMAKE_BUILD_TYPE=Release \
	-DBUILD_SHARED_LIBS=ON \
	-DLLVM_TARGETS_TO_BUILD="all" \
	-G "Unix Makefiles" ..
make -j$(getconf _NPROCESSORS_ONLN) install
popd

pushd keystone/bindings/python
pip install -U -e .
# create a symlink that the python bindings can find the keystone lib
ln -s $(readlink -f ../../../lib/libkeystone.so.0) keystone/libkeystone.so
popd

# kstool doesn't find the lib. so let's rm it
rm -r bin/
